<?php

namespace App\Admin\Controllers;

use App\PlatformBalanceLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class PlatFormAccountController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '账户总览';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PlatformBalanceLog());

        $grid->model()->whereIn('type', [5,9])->orderBy('id', 'desc');

        $grid->header(function ($query) {
            // $allCoin = DB::table('users_balance_logs')->whereIn('type', [1, 2, 3])->where('status', 1)->sum('money');
            $platBalance = DB::table('platform_balance_log')->where('status', 1)->orderBy('id', 'desc')->value('balance');
            $userBalance = DB::table('users')->sum('balance');
            $notSettle = DB::table('users_orders')->whereIn('status', [35,40])->sum('settle_amount');
            return "<div style='font-size: 24px;'>乐淘币总额：<span style='color: black; font-weight: bold;'>".($platBalance+$userBalance)."</span></div>,
<div style='font-size: 20px;'>平台余额：<span style='color: black; font-weight: bold;'>{$platBalance}</span></div>
<div style='border-top: 1px solid black;width: 100%;margin: 0 20px;'></div>
<div style='font-size: 20px;display: inline-block;margin-right: 10px;'>用户余额：<span style='color: black; font-weight: bold;'>{$userBalance}</span></div><div style='font-size: 20px;display: inline-block;'>待结算金额：<span style='color: black; font-weight: bold;'>{$notSettle}</span></div>";
        });

        $grid->column('type_value', '类型');
        $grid->column('created_at', '入账时间');
        $grid->column('title', '名称');
        $grid->column('order_no', '业务单号|支付流水号')->display(function($value){
            return $value.'<br>'.$this->transaction_no;
        });
        $grid->column('account', '账户')->display(function(){return '平台余额';});
        $grid->column('type', '收支类型')->display(function($value){
            if( in_array($value, [1, 6, 7, 9]) ){
                return '收入';
            }else{
                return '支出';
            }
        });
        $grid->column('amount', '额度');
        $grid->column('balance', '余额');
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(PlatFormBalanceLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('type', __('Type'));
        $show->field('type_value', __('Type value'));
        $show->field('title', __('Title'));
        $show->field('order_no', __('Order no'));
        $show->field('transaction_no', __('Transaction no'));
        $show->field('amount', __('Amount'));
        $show->field('balance', __('Balance'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PlatFormBalanceLog());
        $form->radio('type', '类型')->options(['5' => '平台提现', '9'=> '平台充值'])->default(9);
        $form->text('order_no', __('订单号(无需更改)'))->default(getOrderNo(999999, 5));
        $form->text('transaction_no', "微信流水号");
        $form->decimal('amount', '金额')->default(0.00);

        return $form;
    }
}
